//
//  TPNavigationController.m
//  TPHelper
//
//  Created by xuqiang on 2020/7/23.
//  Copyright © 2020 xuqiang. All rights reserved.
//

#import "TPNavigationController.h"
#import "UIColor+Additions.h"
@interface TPNavigationController ()

@end

@implementation TPNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self makeNavigationBarStyle];
}

- (void)makeNavigationBarStyle{
//    [[UINavigationBar appearance] setTintColor:[UIColor grayColor]];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [[UINavigationBar appearance] setBarTintColor:kColorCommonNewBlue];
//    [[UINavigationBar appearance] setBackgroundColor:kColorCommonNewBlue];
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"bg_navigationbar"] forBarMetrics:UIBarMetricsDefault];
    self.navigationBar.translucent = NO;
    [[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
    
    [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIColor dj_colorWithHex:@"#ffffff"], NSForegroundColorAttributeName,                                                           [UIFont systemFontOfSize:17], NSFontAttributeName, nil]];
    //bar在屏幕顶部且它的背景向上延伸进入状态栏区域
    [[UINavigationBar appearance] backgroundImageForBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
//    if (animated) {
//        if (self.shouldIgnorePushingViewControllers) {
//            return;
//        }
//        self.shouldIgnorePushingViewControllers = YES;
//    }
    [super pushViewController:viewController animated:animated];
    
    if (IS_IPHONEX) {
        //防止tabBar在push的时候会忽然变高
        CGRect tabBarFrame = self.tabBarController.tabBar.frame;
        tabBarFrame.origin.y = kScreenHeight - tabBarFrame.size.height;
        self.tabBarController.tabBar.frame = tabBarFrame;
    }
    
    if (self.childViewControllers.count > 1) {
//        if ([viewController isKindOfClass:NSClassFromString(@"TPBaseViewController")]) {
//            UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//            UIImage *backBtnImage = [UIImage imageNamed:@"btn_common_back"];
//            [backBtn setImage:backBtnImage forState:UIControlStateNormal];
//            [backBtn addTarget:viewController action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
//            backBtn.frame = CGRectMake(0, 0, 22, 44);
////            [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -37.5, 0, 0)];
//            UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
//            
//            UIButton *buttonClose = [UIButton buttonWithType:UIButtonTypeCustom];
//            [buttonClose setTitle:@"关闭" forState:UIControlStateNormal];
//            [buttonClose.titleLabel setFont:[UIFont systemFontOfSize:17]];
//            buttonClose.frame = CGRectMake(0, 0, 44, 44);
//            [buttonClose addTarget:viewController action:NSSelectorFromString(@"closeWebView") forControlEvents:UIControlEventTouchUpInside];
//            buttonClose.tag = 100;
//            [buttonClose setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//            UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithCustomView:buttonClose] ;
//            viewController.navigationItem.leftBarButtonItems = @[backButton,closeButton];
//        }else
            if ([viewController isKindOfClass:[UIViewController class]]){
            UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            UIImage *backBtnImage = [UIImage imageNamed:@"btn_common_back"];
            [backBtn setImage:backBtnImage forState:UIControlStateNormal];
            [backBtn addTarget:viewController action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
            backBtn.frame = CGRectMake(0, 0, 44, 44);
            [backBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -37.5, 0, 0)];
            UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
            viewController.navigationItem.leftBarButtonItem = backButton;
        }
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
